/**
 * Copyright &copy; 2016-2017 <a href="https://www.lczyfz.com">联创智云</a> All rights reserved.
 */
package com.lczyfz.campusinfo.modules.jobmesmanage.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lczyfz.campusinfo.modules.jobmesmanage.entity.JobMes;
import com.lczyfz.campusinfo.modules.jobmesmanage.service.JobMesService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.lczyfz.campusinfo.common.config.Global;
import com.lczyfz.campusinfo.common.persistence.Page;
import com.lczyfz.campusinfo.common.web.BaseController;
import com.lczyfz.campusinfo.common.utils.StringUtils;
import com.lczyfz.campusinfo.modules.jobmesmanage.entity.JobWork;
import com.lczyfz.campusinfo.modules.jobmesmanage.service.JobWorkService;

import java.util.List;

/**
 * 就业报名Controller
 * @author zms
 * @version 2018-03-23
 */
@Controller
@RequestMapping(value = "${adminPath}/jobmesmanage/jobWork")
public class JobWorkController extends BaseController {

	@Autowired
	private JobWorkService jobWorkService;
	@Autowired
	private JobMesService jobMesService;
	@ModelAttribute
	public JobWork get(@RequestParam(required=false) String id) {
		JobWork entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = jobWorkService.get(id);
		}
		if (entity == null){
			entity = new JobWork();
		}
		return entity;
	}
	
	@RequiresPermissions("jobmesmanage:jobWork:view")
	@RequestMapping(value = {"list", ""})
	public String list(JobWork jobWork, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<JobWork> page = jobWorkService.findPage(new Page<JobWork>(request, response), jobWork);
		List<JobWork> jobworks = jobWorkService.findList(jobWork);
		model.addAttribute("jobworks", jobworks);
		model.addAttribute("page", page);
		return "modules/campusinfostu/jobWorkList";
	}

	@RequiresPermissions("jobmesmanage:jobWork:view")
	@RequestMapping(value = "form")
	public String form(JobWork jobWork, Model model) {
		JobMes jobMes = new JobMes();
		List<JobMes> jobWorks = jobMesService.findList(jobMes);
		model.addAttribute("jobWork", jobWork);
		model.addAttribute("JobMess", jobWorks);
		return "modules/campusinfostu/jobWorkForm";
	}

	@RequiresPermissions("jobmesmanage:jobWork:edit")
	@RequestMapping(value = "save")
	public String save(JobWork jobWork, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, jobWork)){
			return form(jobWork, model);
		}
		jobWorkService.save(jobWork);
		addMessage(redirectAttributes, "保存就业报名成功");
		return "redirect:"+Global.getAdminPath()+"/jobmesmanage/jobWork/?repage";
	}
	
	@RequiresPermissions("jobmesmanage:jobWork:edit")
	@RequestMapping(value = "delete")
	public String delete(JobWork jobWork, RedirectAttributes redirectAttributes) {
		jobWorkService.delete(jobWork);
		addMessage(redirectAttributes, "删除就业报名成功");
		return "redirect:"+Global.getAdminPath()+"/jobmesmanage/jobWork/?repage";
	}

}